package com.dongge.action;

import com.dongge.entity.Info;
import com.dongge.service.InfoService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/10/26.
 */
@Controller
@Scope("prototype")
public class JsonAction extends ActionSupport implements ModelDriven<Info>,SessionAware {

    private Info info = new Info();

    private List<Info> infos = new ArrayList<Info>();

    public List<Info> getInfos() {
        return infos;
    }

    public void setInfos(List<Info> infos) {
        this.infos = infos;
    }

    public Info getInfo() {
        return info;
    }

    public void setInfo(Info info) {
        this.info = info;
    }

    @Resource
    private InfoService infoService;

    private Map<String,Object> session;


    public String edit(){
        infoService.saveOrUpdate(info);
        return "toList";
    }

    /**
     * 重点处理回显问题
     * @return
     */
    public String editForm(){
        Info info1 = infoService.get(info.getId());
        info.setId(info1.getId());
        info.setAge(info1.getAge());
        info.setName(info1.getName());
        return "edit";
    }

    public String add(){

        infoService.saveOrUpdate(info);
        return "toList";
    }

    public String addForm(){
        return "add";
    }

    public String delete(){

        infoService.delete(info.getId());
        return  "toList";
    }

    public String list(){
        infos = infoService.getList();
        //session.put("list",infos);
        System.out.println("list action:"+new Date());
        return "list";
    }

    public Info getModel() {
        return info;
    }

    public void setSession(Map<String, Object> session) {
        this.session = session;
    }
}
